package ee.siimoliver.chat.GUI.dialogs;

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import ee.siimoliver.chat.GUI.ChatFrame;
import ee.siimoliver.chat.controllers.DialogController;
import ee.siimoliver.chat.utils.ChatUtils;
import ee.siimoliver.chat.utils.Const;

public class ChattableInputDialog extends JDialog implements DialogController {

	private static final long serialVersionUID = 1L;
	private JLabel lblIcon;
	private JLabel lblText;
	private JTextField inputName;
	private JButton btnSearch;
	private JButton btnCancel;
	private ChatFrame actionParent;

	public ChattableInputDialog(Window parent, String text, ImageIcon icon) {
		super(parent);
		parent.setEnabled(false);
		initFields(text, icon);
		addProperties();
		addFields();
		ChatUtils.centerWindowRelativeToParent(this, parent);
	}
	
	public ChattableInputDialog(Window parent, ChatFrame actionParent, String text, ImageIcon icon) {
		this(parent, text, icon);
		actionParent.setEnabled(false);
		this.actionParent = actionParent;
	}
	
	private void addProperties() {
		setUndecorated(true);
		setSize(320, 90);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		((JPanel) getContentPane()).setBorder(BorderFactory.createLineBorder(Color.BLACK));
		ChatUtils.mapActionToEnter(inputName, btnSearch);
		btnSearch.addActionListener(new SearchAction());
		btnCancel.addActionListener(new CancelAction());
	}
	
	private void initFields(String text, ImageIcon icon) {
		lblIcon = new JLabel(icon);
		lblText = new JLabel(text);
		inputName = new JTextField(9);
		btnSearch = new JButton(Const.LBL_SEARCH);
		btnCancel = new JButton(Const.LBL_CANCEL);
	}
	
	private void addFields() {
		setLayout(new MigLayout("center, insets 5", "[fill]", "[grow]"));
		add(lblIcon, "west, c, r");
		add(lblText, "cell 0 0");
		add(inputName, "cell 0 1, span 3");
		add(btnSearch, "cell 1 2, sg");
		add(btnCancel, "cell 2 2, sg");
	}
	
	public void display() {
		setVisible(true);
	}
	
	private void endDialog() {
		getParent().setEnabled(true);
		getParent().setVisible(true);
		ChattableInputDialog.this.dispose();
	}
	
	private class SearchAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (getParent() instanceof ChatFrame) {
				((ChatFrame) getParent()).searchNewChatPartner(inputName.getText(), 
						(Window) getParent());
			} else {
				actionParent.searchNewChatPartner(inputName.getText(), 
						(Window) getParent(), (DialogController) getParent());
			}
			endDialog();
		}
	}
	private class CancelAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			endDialog();
		}
	}
	@Override
	public void updateContent() {
		
	}
}
